﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BicycleTrafficSimulator
{
    class Menu : DrawableGameComponent
    {
        private Texture2D backgroundTexture;

        private Vector2 startButtonPosition;
        private Vector2 exitButtonPosition;
        private Vector2 helpButtonPosition;

        private Rectangle startButtonRect;

        public Rectangle StartButtonRect
        {
            get { return startButtonRect; }
            set { startButtonRect = value; }
        }
        private Rectangle exitButtonRect;

        public Rectangle ExitButtonRect
        {
            get { return exitButtonRect; }
            set { exitButtonRect = value; }
        }
        private Rectangle helpButtonRect;

        public Rectangle HelpButtonRect
        {
            get { return helpButtonRect; }
            set { helpButtonRect = value; }
        }

        MouseState mouseState;
        MouseState previousMouseState;

        private Scene scene;

        public Menu(Scene _scene)
            : base(_scene)
        {
            scene = _scene;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            //set the position of the buttons
            startButtonPosition = new Vector2(1100, 340);
            helpButtonPosition = new Vector2(1100, 400);
            exitButtonPosition = new Vector2(1100, 460);

            startButtonRect = new Rectangle((int)startButtonPosition.X, (int)startButtonPosition.Y, 150, 40);
            exitButtonRect = new Rectangle((int)exitButtonPosition.X, (int)exitButtonPosition.Y, 150, 40);
            helpButtonRect = new Rectangle((int)helpButtonPosition.X, (int)helpButtonPosition.Y, 150, 40);

            //get the mouse state
            mouseState = Mouse.GetState();
            previousMouseState = mouseState;

            backgroundTexture = scene.Content.Load<Texture2D>("background");


        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            //wait for mouseclick
            mouseState = Mouse.GetState();
            if (previousMouseState.LeftButton == ButtonState.Pressed &&
               mouseState.LeftButton == ButtonState.Released)
            {
                MouseClicked(mouseState.X, mouseState.Y);
            }

            previousMouseState = mouseState;
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            base.Draw(gameTime);

            scene.SpriteBatchMenu.Begin();
            scene.SpriteBatchMenu.Draw(backgroundTexture, Vector2.Zero, Color.White);
            scene.SpriteBatchMenu.End();

        }

        void MouseClicked(int x, int y)
        {
            //creates a rectangle of 10x10 around the place where the mouse was clicked
            Rectangle mouseClickRect = new Rectangle(x, y, 10, 10);

            if (mouseClickRect.Intersects(StartButtonRect)) 
            {
                scene.GameStateScene = Scene.GameState.Login;
            }
            else if (mouseClickRect.Intersects(ExitButtonRect)) 
            {
                scene.Exit();
            }
            else if (mouseClickRect.Intersects(HelpButtonRect))
                scene.GameStateScene = Scene.GameState.Instructions;
        }

    }
}
